package top.jybill.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.jybill.dao.IRoleDao;
import top.jybill.dao.IUserDao;
import top.jybill.dao.IUserRoleDao;
import top.jybill.domain.MyPageInfo;
import top.jybill.domain.MyUser;
import top.jybill.domain.Role;
import top.jybill.domain.UserRole;
import top.jybill.exception.MyException;
import top.jybill.service.IUserService;
import top.jybill.utils.CharCodeUtil;
import top.jybill.utils.Md5Util;

import java.util.List;

/**
 * 用户登录服务层
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {

  @Autowired
  private IUserDao userDao;
  @Autowired
  private IUserRoleDao userRoleDao;
  @Autowired
  private IRoleDao roleDao;

  public MyUser findUser(MyUser user) {
    MyUser ret = userDao.findUser(user);

    // 账号密码错误
    if (ret == null) {
      throw new MyException("账号或密码错误");
    }

    return ret;
  }

  /**
   * 分页查用户
   * @param page
   * @return
   * @throws Exception
   */
  public MyPageInfo findUsersByPage(MyPageInfo page) throws Exception {
    // 转UTF-8
    page.setQuery(CharCodeUtil.getUTF8CharCode(page.getQuery(), "UTF-8"));

    // 模糊查询设置
    if (page.getQuery() == null || page.getQuery().length() < 1 || "null".equals(page.getQuery()) || "".equals(page.getQuery())) {
      page.setQuery(null);
    } else {
      page.setQuery("%" + page.getQuery() + "%");
    }

    PageHelper.startPage(page.getPageNum(), page.getPageSize());
    List<MyUser> users= userDao.findUsersByPage(page);
    MyPageInfo retPage= new MyPageInfo(users);
    retPage.setPageNum(page.getPageNum());
    retPage.setPageSize(page.getPageSize());
    return retPage;
  }

  /**
   * 添加用户
   * @param user
   * @throws Exception
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void addUser(MyUser user) throws Exception {
    // 提交的密码加密
    user.setPassword(Md5Util.encodeByMd5(user.getPassword()));

    userDao.addUser(user); // 添加用户

    // 关联外键
    UserRole userRole = new UserRole();
    userRole.setUser(user);
    for (Role role: user.getRoles()) {
      userRole.setRole(role);
      userRoleDao.addUserRole(userRole);
    }
  }

  /**
   * 根据uid删除用户及其关联的角色
   * @param uid
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void delUser(String uid) {
    // 查询关联的角色
    List<Role> roles = roleDao.findRoleById(uid);
    // 删除所有角色
    UserRole userRole = new UserRole();
    MyUser user = new MyUser();
    user.setId(uid);
    userRole.setUser(user);
    for (Role role: roles) {
      userRole.setRole(role);
      userRoleDao.delUserRole(userRole); // 删除关联操作
    }

    // 删除用户
    userDao.delUser(uid);
  }

  /**
   * 更新用户
   * @param user
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void updateUser(MyUser user) throws Exception {
    // 密码MD5加密
    user.setPassword(Md5Util.encodeByMd5(user.getPassword()));

    userDao.updateUser(user); // 跟新用户基本信息

    // 关联用户操作
    userRoleDao.delUserRoleByuid(user.getId()); // 删除
    userRoleDao.addUserRoleByUser(user); // 添加关联
  }

  /**
   * 根据id查询用户
   * @param uid
   * @return
   */
  public MyUser findUserByuid(String uid) {
    return userDao.findUserByuid(uid);
  }

}
